<?php
namespace app\controller\api\v1\housing;
use app\common\model\user\User;
use app\model\order\StoreOrder;
use app\model\v1\gift\GiftCard;
use app\model\v1\gift\GiftCardNumber;
use app\model\v1\gift\GiftCardUser;
use app\model\v1\recovery\RecoveryUserSearchHistory;
use app\Request;
use app\services\order\StoreOrderCreateServices;
use app\services\user\UserServices;
use think\facade\Db;
use app\validate\api\housing\HousingSourceValidate;
use app\validate\api\housing\HousingSource1Validate;
use app\validate\api\housing\HousingSource2Validate;
use app\validate\api\housing\HousingSource3Validate;
use app\validate\api\housing\HousingSource4Validate;
use app\validate\api\housing\HousingSourceofficeValidate;
use app\validate\api\housing\HousingSourceoffice1Validate;
use app\validate\api\housing\HousingSourceoffice2Validate;
use app\validate\api\housing\HousingSourceoffice3Validate;

class HousingSource
{
    //  1商铺出租 2商铺买卖  3商铺分组 4生意转让

    //  1写字楼出租 2写字楼买卖  3资管房源 4联合办公
	public function housing_source_add (Request $request)
	{
		$request_data = $request->post();

		validate(HousingSourceValidate::class)->check($request_data);
		switch ($request_data['housing_type']) {
			case 1:
				//商铺
				if ($request_data['type']==1){
					//商铺出租
					validate(HousingSource1Validate::class)->check($request_data);
				}
				if ($request_data['type']==2){
					//商铺买卖
					validate(HousingSource2Validate::class)->check($request_data);
				}
				if ($request_data['type']==3){
					//商铺分组
					validate(HousingSource3Validate::class)->check($request_data);
				}if ($request_data['type']==4){
					//生意转让
					validate(HousingSource4Validate::class)->check($request_data);
				}
				break;
			case 2:
				//写字楼
				if ($request_data['type']==1){
					//出租
					validate(HousingSourceofficeValidate::class)->check($request_data);
				}if ($request_data['type']==2){
					//写字楼出售模板
					validate(HousingSourceoffice1Validate::class)->check($request_data);
				}
				if ($request_data['type']==3){
					//资管房源
					validate(HousingSourceoffice2Validate::class)->check($request_data);
				}
				if ($request_data['type']==4){
					//联合办公
					validate(HousingSourceoffice3Validate::class)->check($request_data);
				}

				break;
			default;
		}

		$data =$request_data;
		$data['status'] =1;
		$data['views']=0;
		$data['user_id']=$request->uid;
		$data['creatdate']=date('Y-m-d H:i:s');
		if (empty($request_data['is_proprietor'])){
			return app('json')->fail('is_proprietor字段不能为空');
		}
		unset($data['housing_label']);
		unset($data['matching_setting']);
		unset($data['passenger_flow_crowd']);
		unset($data['Joint_office']);
		try {
			Db::startTrans();
			$Model = new  \app\model\v1\housing\HousingSource();
			$housing_id=$Model->insertGetId ($data);
			if (!empty($request_data['housing_label'])){
				//房源标签
				$housing_label = explode (',',$request_data['housing_label']);
				$housing_label_data=[];
				foreach ($housing_label as $k=>$v){
					$housing_label_data[$k]['housing_source_id']=$housing_id;
					$housing_label_data[$k]['housing_label_id']=$v;
					$housing_label_data[$k]['ctime']=date('Y-m-d H:i:s');
				}

				$HousingHousingLabelAssociation = new  \app\model\v1\housing\HousingHousingLabelAssociation();
				$HousingHousingLabelAssociation->insertAll ($housing_label_data);
			}

			if (!empty($request_data['matching_setting'])){
				//配套设施_房产
				$matching_setting = explode (',',$request_data['matching_setting']);
				$matching_setting_data=[];
				foreach ($matching_setting as $k=>$v){
					$matching_setting_data[$k]['matching_setting_id']=$v;
					$matching_setting_data[$k]['housing_source_id']=$housing_id;
					$matching_setting_data[$k]['ctime']=date('Y-m-d H:i:s');
				}
				$HousingMatchingSourceAssociation = new  \app\model\v1\housing\HousingMatchingSourceAssociation();
				$HousingMatchingSourceAssociation->insertAll ($matching_setting_data);
			}
			//客流
//			if (!empty($request_data['passenger_flow_crowd'])){
//				//客流
//				$passenger_flow_crowds=[];
//				$passenger_flow_crowd = explode (',',$request_data['passenger_flow_crowd']);
//				foreach ($passenger_flow_crowd as $k=>$v){
//					$passenger_flow_crowds[$k]['matching_setting_id']=$housing_id;
//					$passenger_flow_crowds[$k]['passenger_flow_crowd_id']=$v;
//					$passenger_flow_crowds[$k]['ctime']=date('Y-m-d H:i:s');
//				}
//				$HousingPassengerFlowCrowdAssociation = new  \app\model\v1\housing\HousingPassengerFlowCrowdAssociation();
//				$HousingPassengerFlowCrowdAssociation->insertAll ($passenger_flow_crowds);
//			}
			//联合办公
			if (!empty($data['Joint_office'])){
				$Joint_office = json_decode ($data['Joint_office'],true);
				$Joint_office_data=[];
				foreach ($Joint_office as $k=>$v){
					$Joint_office_data[$k]['title_name']=$v['title_name'];  //标题
					$Joint_office_data[$k]['Joint_office_img']=$v['Joint_office_img'];  //图片
					$Joint_office_data[$k]['Joint_office_is_negotiate']=$v['Joint_office_is_negotiate']; //是否面议
					$Joint_office_data[$k]['Joint_office_price']=$v['Joint_office_price'];  //租金
					$Joint_office_data[$k]['Joint_office_delineate_pirce']=$v['Joint_office_delineate_pirce'];//划线价
					$Joint_office_data[$k]['Joint_office_specifications']=$v['Joint_office_specifications'];//规格
				}
				$HousingJointOffice = new  \app\model\v1\housing\HousingJointOffice();
				$HousingJointOffice ->where ('housing_source_id',$housing_id)->delete ();

				$HousingJointOffice->insertAll ($Joint_office_data);
			}
			Db::commit();
		} catch (\Exception $e) {
			Db::rollback();
			return app('json')->fail($e->getMessage());
		}
			return app('json')->status('success', '提交成功');
	}

	public function housing_source_index (Request $request)
	{
		$request_data = $request->post();
		if (empty($request_data['housing_source_id'])){
			return app('json')->fail('housing_source_id不能为空');
		}
			$Model = new  \app\model\v1\housing\HousingSource();

			$data =$Model->Housing_index($request_data['housing_source_id'],$request->uid);



		if ($data){
			if ($data->user_id!=$request->uid()){
			$Model->where ('id',$request_data['housing_source_id'])->inc('views',1)->update ();
			}
			return app('json')->status('success', '成功',$data);

		}else{
			return app('json')->fail('提交失败');
		}
	}

	public function housing_source_list (Request $request)
	{
		$request_data = $request->post();

		$limit = $request_data['limit'] ?? 10;
		$page = $request_data['page'] ?? 1;

		$Model = new  \app\model\v1\housing\HousingSource();
		$data['data'] =$Model::field('id,housing_source_door_head_img,housing_source_title,architecture_area,architecture_rent,transfer_fee,region,type,region_name,housing_type')
			->with ('street')
			->with ('MetroStation')
			->with ('HousingMatchingSourceAssociation')
			->with ('HousingHousingLabelAssociation')
			->where(function ($query) use ($request_data){
			if(!empty($request_data['type']))$query->where('type', $request_data['type']);   //发布类型
			if(!empty($request_data['is_proprietor']))$query->where('is_proprietor', $request_data['is_proprietor']);//是否是业主
			if(!empty($request_data['region']))$query->where('region', 'like',$request_data['region']."%"); //区域
			if(!empty($request_data['metro_station_id']))$query->where('metro_station_id', $request_data['metro_station_id']);//地铁站
			if(!empty($request_data['suitable_for_business_form']))$query->where('suitable_for_business_form', $request_data['suitable_for_business_form']);//行业
//			if(!empty($request_data['shop_type']))$query->where('shop_type', $request_data['shop_type']);//房源类型
			if(!empty($request_data['housing_type']))$query->where('housing_type', $request_data['housing_type']);//房源类型
//			if(!empty($request_data['architecture_area']))$query->where('architecture_area','BETWEEN', $request_data['architecture_area']);//建筑面积
//			if(!empty($request_data['architecture_rent']))$query->where('architecture_rent','BETWEEN', $request_data['architecture_rent']);//租金
			if(!empty($request_data['housing_source_title']))$query->where('housing_source_title','like', '%'.$request_data['housing_source_title'].'%');
			})
			->wherein ('status',[3,4])
			->order ('status','desc')
			->page ($page,$limit)
			->select ();
		if(!empty($request_data['housing_source_title'])){
			RecoveryUserSearchHistory::create([
				'uid' => $request->uid(),
				'topic' => 5,
				'keyword' => $request_data['housing_source_title'],
			]);
		}
		$data['count'] =$Model
			->where(function ($query) use ($request_data){
				if(!empty($request_data['type']))$query->where('type', $request_data['type']);   //发布类型    1=转让   2 租赁 3写字楼
				if(!empty($request_data['is_proprietor']))$query->where('is_proprietor', $request_data['is_proprietor']);//是否是业主
				if(!empty($request_data['region']))$query->where('region', 'like',$request_data['region']."%"); //区域
				if(!empty($request_data['metro_station_id']))$query->where('metro_station_id', $request_data['metro_station_id']);//地铁站
				if(!empty($request_data['suitable_for_business_form']))$query->where('suitable_for_business_form', $request_data['suitable_for_business_form']);//行业
				if(!empty($request_data['shop_type']))$query->where('shop_type', $request_data['shop_type']);//房源类型
				if(!empty($request_data['housing_type']))$query->where('housing_type', $request_data['housing_type']);//房源类型
				if(!empty($request_data['architecture_area']))$query->where('architecture_area','BETWEEN', $request_data['architecture_area']);//建筑面积
				if(!empty($request_data['architecture_rent']))$query->where('architecture_rent','BETWEEN', $request_data['architecture_rent']);//租金
				if(!empty($request_data['housing_source_title']))$query->where('housing_source_title','like', '%'.$request_data['housing_source_title'].'%');
			})
			->wherein ('status',[3,4])
			->count ();
		return app('json')->status('success', '成功',$data);

	}
	//猜你喜欢
	public function housing_like_list (Request $request)
	{
		$request_data = $request->post();

		if (empty($request_data['housing_id'])){
			return app('json')->fail('housing_source_id不能为空');
		}
		$limit = $request_data['limit'] ?? 10;
		$page = $request_data['page'] ?? 1;

		$Model = new  \app\model\v1\housing\HousingSource();
		$housing = $Model->where ('id',$request_data['housing_id'])->find ();
		if (!$housing){
			return app('json')->fail('未找到此房源');
		}
		$data['data'] =$Model::field('id,housing_source_door_head_img,housing_source_title,architecture_area,architecture_rent,transfer_fee,region,region_name,type,housing_type')
			->with ('street')
			->with ('MetroStation')
			->with ('HousingMatchingSourceAssociation')
			->with ('HousingHousingLabelAssociation')
			->wherein ('status',[3,4])
			->where ('id','<>',$request_data['housing_id'])
			->order ('views','desc')
			->where(function ($query) use ($housing) {
			if (empty($housing['shop_type']))$query->where('shop_type',$housing['shop_type']);
			if (empty($housing['region']))$query->where('region',$housing['region']);
			if (empty($housing['type']))$query->where('type',$housing['type']);
			if (empty($housing['housing_type']))$query->where('housing_type',$housing['housing_type']);
			})
			->page ($page,$limit)
			->select ();
		$data['count'] =$Model
			->wherein ('status',[3,4])
			->where ('id','<>',$request_data['housing_id'])
			->where(function ($query) use ($housing) {
				if (empty($housing['shop_type']))$query->where('shop_type',$housing['shop_type']);
				if (empty($housing['region']))$query->where('region',$housing['region']);
				if (empty($housing['type']))$query->where('type',$housing['type']);
				if (empty($housing['housing_type']))$query->where('housing_type',$housing['housing_type']);
			})
			->count  ();
		return app('json')->status('success', '成功',$data);
	}

	//房源编辑
	public function housing_source_up (Request $request)
	{
		$request_data = $request->post();

		validate(HousingSourceValidate::class)->check($request_data);
		switch ($request_data['housing_type']) {
			case 1:
				//商铺
				if ($request_data['type']==1){
					//商铺出租
					validate(HousingSource1Validate::class)->check($request_data);
				}
				if ($request_data['type']==2){
					//商铺买卖
					validate(HousingSource2Validate::class)->check($request_data);
				}
				if ($request_data['type']==3){
					//商铺分组
					validate(HousingSource3Validate::class)->check($request_data);
				}if ($request_data['type']==4){
				//生意转让
				validate(HousingSource4Validate::class)->check($request_data);
			}
				break;
			case 2:
				//写字楼
				if ($request_data['type']==1){
					//出租
					validate(HousingSourceofficeValidate::class)->check($request_data);
				}if ($request_data['type']==2){
				//写字楼出售模板
				validate(HousingSourceoffice1Validate::class)->check($request_data);
			}
				if ($request_data['type']==3){
					//资管房源
					validate(HousingSourceoffice2Validate::class)->check($request_data);
				}
				if ($request_data['type']==4){
					//联合办公
					validate(HousingSourceoffice3Validate::class)->check($request_data);
				}

				break;
			default;
		}

		$data =$request_data;
		if (empty($request_data['is_proprietor'])){
			return app('json')->fail('is_proprietor字段不能为空');
		}
		$Model = new  \app\model\v1\housing\HousingSource();

		$housing_id=$request_data['id'];

		try {
			Db::startTrans();

			$res =$Model->where ('id',$housing_id)->update($data);
			if ($res){
				$datas['status']=1;
				$Model->where ('id',$housing_id)->update($datas);
			}else{
				Db::rollback();
				return app('json')->fail('未更改数据');
			}

			if (!empty($data['housing_label'])){
				//房源标签
				$housing_label = explode (',',$data['housing_label']);
				$housing_label_data=[];
				foreach ($housing_label as $k=>$v){
					$housing_label_data[$k]['housing_source_id']=$housing_id;
					$housing_label_data[$k]['housing_label_id']=$v;
					$housing_label_data[$k]['ctime']=date('Y-m-d H:i:s');
				}

				$HousingHousingLabelAssociation = new  \app\model\v1\housing\HousingHousingLabelAssociation();
				$HousingHousingLabelAssociation ->where ('housing_source_id',$housing_id)->delete ();
				$HousingHousingLabelAssociation->insertAll ($housing_label_data);
			}
//			if (!empty($data['passenger_flow_crowd'])){
//				//客流
//				$passenger_flow_crowds=[];
//				$passenger_flow_crowd = explode (',',$data['passenger_flow_crowd']);
//				foreach ($passenger_flow_crowd as $k=>$v){
//					$passenger_flow_crowds[$k]['matching_setting_id']=$housing_id;
//					$passenger_flow_crowds[$k]['passenger_flow_crowd_id']=$v;
//					$passenger_flow_crowds[$k]['ctime']=date('Y-m-d H:i:s');
//				}
//				$HousingPassengerFlowCrowdAssociation = new  \app\model\v1\housing\HousingPassengerFlowCrowdAssociation();
//				$HousingPassengerFlowCrowdAssociation ->where ('matching_setting_id',$housing_id)->delete ();
//				$HousingPassengerFlowCrowdAssociation->insertAll ($passenger_flow_crowds);
//			}


			if (!empty($data['matching_setting'])){
				//配套设施
				$matching_setting = explode (',',$data['matching_setting']);
				$matching_setting_data=[];
				foreach ($matching_setting as $k=>$v){
					$matching_setting_data[$k]['matching_setting_id']=$v;
					$matching_setting_data[$k]['housing_source_id']=$housing_id;
					$matching_setting_data[$k]['ctime']=date('Y-m-d H:i:s');
				}
				$HousingMatchingSourceAssociation = new  \app\model\v1\housing\HousingMatchingSourceAssociation();
				$HousingMatchingSourceAssociation ->where ('housing_source_id',$housing_id)->delete ();

				$HousingMatchingSourceAssociation->insertAll ($matching_setting_data);
			}
			if (!empty($data['Joint_office'])){
				//配套设施
				$Joint_office = json_decode ($data['Joint_office'],true);
				$Joint_office_data=[];
				foreach ($Joint_office as $k=>$v){
					$Joint_office_data[$k]['title_name']=$v['title_name'];
					$Joint_office_data[$k]['Joint_office_img']=$v['Joint_office_img'];
					$Joint_office_data[$k]['Joint_office_is_negotiate']=$v['Joint_office_is_negotiate'];
					$Joint_office_data[$k]['Joint_office_price']=$v['Joint_office_price'];
					$Joint_office_data[$k]['Joint_office_delineate_pirce']=$v['Joint_office_delineate_pirce'];
					$Joint_office_data[$k]['Joint_office_specifications']=$v['Joint_office_specifications'];
				}
				$HousingJointOffice = new  \app\model\v1\housing\HousingJointOffice();
				$HousingJointOffice ->where ('housing_source_id',$housing_id)->delete ();

				$HousingJointOffice->insertAll ($Joint_office_data);
			}



			Db::commit();
		} catch (\Exception $e) {
			Db::rollback();
			return app('json')->fail($e->getMessage());
		}
			return app('json')->status('success', '修改成功');
	}
	//我的发布
	public function user_housing_source_list (Request $request)
	{

		$request_data = $request->post();


		$limit = $request_data['limit'] ?? 10;
		$page = $request_data['page'] ?? 1;
		$Model = new  \app\model\v1\housing\HousingSource();
		$data['data'] =$Model::field('housing_type,id,housing_source_door_head_img,architecture_area,architecture_rent,housing_source_title,type,is_proprietor,status,shop_type,region_name,total')
			->with ('HousingShopType')
			->with('street')
			->with ('MetroStation')
			->with ('HousingHousingLabelAssociation')
			->where(function ($query) use ($request_data,$request) {

				if (!empty($request_data[ 'housing_type' ])) $query -> where ('housing_type', $request_data[ 'housing_type' ]);   //发布类型  1商铺 2写字楼


				if(!empty($request_data['status'])){
					if ($request_data['status']==1)$query->wherein('status',['1',6]);   //待审核   审核失败  和 审核中的
					if ($request_data['status']==2)$query->where('status',2);   //待发布
					if ($request_data['status']==3)$query->where('status',3);   //已发布
					if ($request_data['status']==4)$query->where('status',4);   //推广中
					if ($request_data['status']==5)$query->where('status',5);   //已下架
				}

				$query -> where ('user_id', $request->uid);
				if(!empty($request_data['is_proprietor'])){
					$query -> where ('is_proprietor', $request_data['is_proprietor']);
				}
			})
			->page ($page,$limit)
			->order ('id desc')
			->select ();
		$data['count'] =$Model
			->where(function ($query) use ($request_data,$request){
				if (!empty($request_data[ 'housing_type' ])) $query -> where ('housing_type', $request_data[ 'housing_type' ]);   //发布类型  1商铺 2写字楼

				if(!empty($request_data['status'])){
					if ($request_data['status']==1)$query->wherein('status',['1',6]);   //待审核   审核失败  和 审核中的
					if ($request_data['status']==2)$query->where('status',2);   //待发布
					if ($request_data['status']==3)$query->where('status',3);   //已发布
					if ($request_data['status']==4)$query->where('status',4);   //推广中
					if ($request_data['status']==5)$query->where('status',5);   //已下架
				}

				$query -> where ('user_id', $request->uid);

				if(!empty($request_data['is_proprietor'])){

					$query -> where ('is_proprietor', $request_data['is_proprietor']);
				}

			})
			->count ();
		return app('json')->status('success', '成功',$data);


	}
	//房源推荐
	public function user_Recommend_list (request $request)
	{

		$request_data = $request->post();

		$limit = $request_data['limit'] ?? 10;
		$page = $request_data['page'] ?? 1;
		$Model = new  \app\model\v1\housing\HousingSource();

		$data['data'] =$Model::field('id,housing_source_door_head_img,housing_source_title,architecture_area,architecture_rent,transfer_fee,region,region_name,status,is_up')
			->with('street')
			->with ('MetroStation')
			->with ('HousingMatchingSourceAssociation')
			->with ('HousingHousingLabelAssociation')
			->wherein ('status',3,5)
			->order (['views'=>'desc','creatdate'=>'desc'])
			->page ($page,$limit)
			->select ();
		$data['count'] =$Model
			->wherein ('status',3,5)
			->count ();
		return app('json')->status('success', '成功',$data);

	}
	//房产编辑上下架
	public function housing_source_isup (Request $request)
	{
		$request_data = $request->post();

		$data =$request_data;
		$housing_id=$request_data['id'];

		try {

			Db::startTrans();
			$Model = new  \app\model\v1\housing\HousingSource();

			$HousingSource=$Model->where ('id',$housing_id)->field ('status')->lock (true)->find ();

			if ($HousingSource['status']==3||$HousingSource['status']==4){
				$Model->where ('id',$housing_id)->update('status',5);
			}elseif ($HousingSource['status']==2&&$data['status']==5){
				$Model->where ('id',$housing_id)->update('status',3);
			}else{
				return  app('json')->fail('当前不能上下架');
			}
			Db::commit();
		} catch (\Exception $e) {
			Db::rollback();
			return  app('json')->fail($e->getMessage());
		}
		return  app('json')->status('成功');
	}
	public function housing_source_del  (Request $request)
	{
		$request_data = $request->post();

		$data =$request_data;
		$housing_id=$request_data['id'];

		unset($data['id']);
		try {

			Db::startTrans();
			$Model = new  \app\model\v1\housing\HousingSource();
			$HousingSource=$Model->where ('id',$housing_id)->delete ();
			Db::commit();
		} catch (\Exception $e) {
			Db::rollback();
			$results['msg'] = $e->getMessage();
			$results['status'] = false;
			return json_encode($results);
		}

		Ajaxreturn::return_succes (true,'成功');

	}

	//此用户下的所有房源
	public function housing_user_list (Request $request)
	{
		$request_data = $request->post();

		$limit = $request_data['limit'] ?? 10;
		$page = $request_data['page'] ?? 1;

		$Model = new  \app\model\v1\housing\HousingSource();

		$data['data'] =$Model::field('id,housing_source_door_head_img,housing_source_title,architecture_area,architecture_rent,transfer_fee,region,type,region_name')
		->with ('street')
		->with ('MetroStation')
		->with ('HousingMatchingSourceAssociation')
		->with ('HousingHousingLabelAssociation')
			->where(function ($query) use ($request_data) {
				if(!empty($request_data['status']))$query->wherein('status',[3,4]);   //上下架
			})
			->order ('views','desc')
			->where ('user_id',$request_data['user_id'])
			->page ($page,$limit)
			->select ();
		$data['count'] =$Model
			->where(function ($query) use ($request_data) {
				if(!empty($request_data['status']))$query->wherein('status',[3,4]);   //上下架
			})
			->order ('views','desc')
			->where ('user_id',$request_data['user_id'])
			->count  ();
		Ajaxreturn::return_succes (true,'成功',$data);
	}
	//推广
	public function housing_source_spread(Request $request)
	{
		$request_data = $request->post();

		$data =$request_data;
		$housing_id=$request_data['id'];
		if (empty($data['promotion_integral'])||!is_numeric ($data['promotion_integral'])){
			return  app('json')->fail('推广投入积分输入错误');
		}
		if (empty($data['spread_integral'])||!is_numeric ($data['spread_integral'])){
			return  app('json')->fail('推广消耗积分输入错误');
		}
		if ($data['promotion_integral']<$data['spread_integral']){
			return   app('json')->fail('推广投入积分不能小于推广消耗积分');
		}
		try {

			Db::startTrans();
			$Model = new  \app\model\v1\housing\HousingSource();

			$HousingSource=$Model->where ('id',$housing_id)->where ('user_id',$request->uid())->lock (true)->find ();
				if (!$HousingSource){
					Db::rollback();
					return  app('json')->fail('当前房源不存在');
				}
				if ($HousingSource['status']==4){
					Db::rollback();
					return  app('json')->fail('当前在推广中，如需更改请先下架推广');
				}
				if ($HousingSource['status']==3) {
						$Model -> where ('id', $housing_id) -> update (['status' => 4, 'promotion_integral' => $data[ 'promotion_integral' ], 'spread_integral' => $data[ 'spread_integral' ], 'remain_integral' => $data[ 'promotion_integral' ]]);
					$user =Db::name('user')->where("uid", $request->uid())->lock(true)->find();
					if ($user['integral']<$data['promotion_integral'])return app('json')->fail('积分不足');
						Db::name('user')->where("uid", $request->uid())->dec('integral', $data["promotion_integral"])->update();
					$add = [
						'uid' => $HousingSource['user_id'],
						'link_id' => $HousingSource['id'],
						'pm' => 0,
						'title' => '推广房源使用积分',
						'category' => 'integral',
						'type' => 'member_pay_num',
						'number' => $data["promotion_integral"],
						'balance' => $user['integral'] - $data["promotion_integral"],
						'mark' => '推广房源使用'.$data["promotion_integral"].'积分',
						'add_time' => date("Y-m-d H:i:s"),
					];

					Db::name('user_bill')->insert($add);


				}else{
					Db::rollback();
					return  app('json')->fail('当前不能推广');
				}
			Db::commit();
		} catch (\Exception $e) {
			Db::rollback();
			return  app('json')->fail($e->getMessage());
		}
		return  app('json')->status('成功');
	}
	//下架推广
	public function housing_source_spread_discontinue(Request $request)
	{
		$request_data = $request->post();
		$housing_id=$request_data['id'];
		try {

			Db::startTrans();
			$Model = new  \app\model\v1\housing\HousingSource();

			$HousingSource=$Model->where ('id',$housing_id)->where ('user_id',$request->uid())->lock (true)->find ();
			if (!$HousingSource){
				Db::rollback();
				return  app('json')->fail('当前房源不存在');
			}
			if ($HousingSource['status']!=4){
				Db::rollback();
				return  app('json')->fail('当前在房源不在推广中');
			}
			if ($HousingSource['status']==4){
				$Model->where ('id',$housing_id)->update(['status'=>3,'promotion_integral'=>0,'spread_integral'=>0,'remain_integral'=>0]);

				$user =Db::name('user')->where("uid", $request->uid())->lock(true)->find();
				if ($HousingSource[ 'remain_integral' ] > 0) {
					$add = [
						'uid' => $request -> uid,
						'link_id' => $HousingSource['id'],
						'pm' => 1,
						'title' => '下架房源推广退还积分',
						'category' => 'integral',
						'type' => 'member_pay_num',
						'number' => $HousingSource[ 'remain_integral' ],
						'balance' => $user[ 'integral' ] + $HousingSource[ 'remain_integral' ],
						'mark' => '下架房源推广退回积分' . $HousingSource[ 'remain_integral' ],
						'add_time' => date ("Y-m-d H:i:s"),
					];
					Db::name('user')-> where ("uid", $request->uid()) ->inc ('integral', $HousingSource[ "remain_integral" ]) -> update ();
					$add[ 'add_time' ] = time ();
					Db ::name ('eb_user_bill') -> insert ($add);
				}
			}else{
				Db::rollback();
				return  app('json')->fail('当前不能推广');
			}
			Db::commit();
		} catch (\Exception $e) {
			Db::rollback();
			return  app('json')->fail($e->getMessage());
		}
		return  app('json')->status('成功');
	}

	public function Integral_configure(Request $request)
	{
		$data = Db::name('lntegral_configure')->where('status',1)->select();

		return  app('json')->status(true,'成功',$data);
	}

	public function create_intergral_order(Request $request)
	{
		$param = $request->post();
		$data = Db::name('lntegral_configure')->where('status',1)->where('id',$param['lntegral_id'])->find();

		$orderService = app()->make(StoreOrderCreateServices::class);

		$user_info = \app\model\user\User ::where("uid", $request->uid ())->find();
		$order_id = $orderService->getNewOrderId();
		try {
			Db::startTrans();
			//创建礼品卡订单
			$add = [
				'type' => 32,  //订单类型
				'order_id' => $order_id,  //订单id
				'uid' => $request->uid (),
				'real_name' => $addressInfo['real_name'] ?? $user_info['real_name'],
				'unique' => md5(microtime()),
				'user_phone' => $addressInfo['phone'] ?? $user_info['phone'],
				'user_address' =>'',
				'address_id' => $param['address_id'] ?? '',
				'total_price' => $data['price'],
				'pay_price' => $data['price'],
				'integration_get' => $data['Integral'],
				'status' => 0,
				'paid' => 0,
				'add_time' => time(),
			];
			$res = StoreOrder::create($add);
			Db::commit();

		} catch (\Exception $e) {
			Db ::rollback ();
			return app ('json') -> fail ($e -> getMessage ());
		}
		return app('json')->status(true,'成功',$add);
	}

	//举报房源
	public function report_housing_add(Request $request){
		$param = $request->post();
		$data =DB::name('report_housing')->where('housing_source_ids',$param['housing_source_ids'])->where('uid',$request->uid())->find();
		if ($data){
			return app('json')->fail('您已经举报过该房源');
		}
		$add = [
			'housing_source_ids' => $param['housing_source_ids'],
			'report_comment' => $param['report_comment'],
			'report_label' => $param['report_label'],
			'report_img' => $param['report_img'],
			'uid' => $request->uid (),
			'c_time' => date ("Y-m-d H:i:s"),
			];
		DB::name('report_housing')->insert($add);
		return app('json')->status(true,'成功',$add);
	}
}
